package models;

import java.util.*;

import siena.*;
import util.DeckImporterExporter;

public class Deck extends Model {

    @Id
    public Long id;

    public String user;
    public String name;
    public String notes;

    public Deck(String user, String name) {
        this.user = user;
        this.name = name;
        this.notes = "";
    }

    private static Query<Deck> all() {
        return Model.all(Deck.class);
    }

    public static Collection<Deck> findAll() {
        return all().fetch();
    }

    public static Deck findById(Long id) {
        return all().filter("id", id).get();
    }

    public static Collection<Deck> findByUser(String user) {
        return all().filter("user", user).fetch();
    }

    @Override
    public void delete() {
        for (Card card : getCards()) {

            card.delete();
        }
        super.delete();
    }

    public Collection<Card> getCards() {
        return Card.findByDeck(this);
    }

    public String toString() {
        return name;
    }

    public String toText() {
        return DeckImporterExporter.toText(this);
    }

    public static Deck fromText(String user, String text) {
        return DeckImporterExporter.fromText(user, text);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        Deck deck = (Deck) o;

        if (id != null ? !id.equals(deck.id) : deck.id != null) return false;
        if (name != null ? !name.equals(deck.name) : deck.name != null) return false;
        if (notes != null ? !notes.equals(deck.notes) : deck.notes != null) return false;
        if (user != null ? !user.equals(deck.user) : deck.user != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (id != null ? id.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (notes != null ? notes.hashCode() : 0);
        return result;
    }
}

